#include "glfw_window.h"

#include <GLFW/glfw3.h>

#include "glfw_monitor.h"

namespace veng {

Window::Window(gsl::czstring name, glm::ivec2 size) {
  // 限定窗口不能改变大小
  //glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
  // 这个是限制不使用OpenGL，因为需要Vulkan
  glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);

  // 创建窗口
  window_ = glfwCreateWindow(size.x, size.y, name, nullptr, nullptr);
  if (window_ == nullptr) {
    std::cout << "Window initialize failed." << std::endl;
  } else {
    std::cout << "Window initialize success." << std::endl;
  }
}

Window::~Window() {
  glfwDestroyWindow(window_);
}

glm::ivec2 Window::GetWindowSize() const {
  glm::ivec2 size;
  glfwGetWindowSize(window_, &size.x, &size.y);
  return size;
}

glm::ivec2 Window::GetFrameBufferSize() const {
  glm::ivec2 size;
  glfwGetFramebufferSize(window_, &size.x, &size.y);
  return size;
}

bool Window::ShouldClose() const {
  return glfwWindowShouldClose(window_);
}

GLFWwindow* Window::GetHandle() const {
  return window_;
}

bool Window::TryMoveToMonitor(std::uint16_t monitor_number) {
  // 使窗口置中，这里的monitors[0]应该是monitors[1]，即取第二个显示器。然后把窗口显示到第二个显示器上面去。但我没有第二个显示器。
  auto monitors = veng::GetMonitors();
  veng::MoveWindowToMonitor(window_, monitors[monitor_number]);
  return true;
}

}  // namespace veng
